package co.fitstart.admin.web.controller.content;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import co.fitstart.admin.service.DataService;
import co.fitstart.admin.web.interceptor.MenuSetting;
import co.fitstart.entity.content.Module;

import com.demo2do.core.support.Result;
import com.demo2do.core.utils.JsonUtils;

/**
 * 
 * @author lidan
 * @author mohanlan
 *
 */
@Controller
@RequestMapping("/modules")
@MenuSetting("menu-content")
public class ModuleController {
    
    private static final Logger logger = LoggerFactory.getLogger(ModuleController.class);

    @Autowired
    @Qualifier("dataService")
    private DataService<Module> moduleService;
    
    /**
     * List all modules
     * 
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET)
    @MenuSetting("submenu-module-management")
    public ModelAndView list(){
        
        ModelAndView modelAndView = new ModelAndView("content/module/module-list");
        
        List<Module> modules = moduleService.listAll(Module.class);
        
        modelAndView.addObject("modules", modules);
        
        return modelAndView;
    }
    
    /**
     * Display create panel
     * 
     * @return
     */
    @RequestMapping(value = "/create", method = RequestMethod.GET)
    public String create(){
        return "content/module/module-create";
    }
    
    /**
     * Create a new module
     * 
     * @param module
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.POST)
    public @ResponseBody String onCreate(Module module){

        Result result = new Result();
        
        try {
            
            moduleService.save(module.initialize());
            
            result.success().message("创建文章模块成功");
            
        } catch (Exception e) {
            
            logger.error("ModuleController#onCreate() error:", e);
            
            result.fail().message("创建文章模块失败");
        }
        
        return JsonUtils.toJsonString(result);
    }
    
    /**
     * @param moduleId
     * @return
     */
    @RequestMapping(value="/{moduleId}", method=RequestMethod.GET)
    public ModelAndView view(@PathVariable("moduleId") Long moduleId){
        
        ModelAndView modelAndView = new ModelAndView("content/module/module-view");
        
        Module module = moduleService.load(Module.class, moduleId);
        
        modelAndView.addObject("module", module);
        
        return modelAndView;
    }
    
    /**
     * Do toggle module
     * 
     * @param moduleId
     * @return
     */
    @RequestMapping(value="/{moduleId}", method=RequestMethod.PATCH)
    public @ResponseBody String onToggleDisplay(@PathVariable("moduleId") Long moduleId){
        
        Result result = new Result();
        
        try {
            
            Module module = moduleService.load(Module.class, moduleId);
            
            moduleService.update(module.toggleDisplay());
            
            result.success().message("更改文章模块显示状态成功");
            
        } catch (Exception e) {
            
            logger.error("ModuleController#onToggleDisplay() error:", e);
            
            result.fail().message("更改文章模块显示状态失败");
        }
        
        return JsonUtils.toJsonString(result);
    }
    
    /**
     * Display delete panel
     * 
     * @param moduleId
     * @return
     */
    @RequestMapping(value = "/{moduleId}/delete", method = RequestMethod.GET)
    public ModelAndView delete(@PathVariable("moduleId") Long moduleId){

        ModelAndView modelAndView = new ModelAndView("content/module/module-delete");
        
        modelAndView.addObject("moduleId", moduleId);
        
        return modelAndView;
    }
    
    /**
     * Do delete module
     * 
     * @param moduleId
     * @return
     */
    @RequestMapping(value="/{moduleId}", method=RequestMethod.DELETE)
    public @ResponseBody String onDelete(@PathVariable("moduleId") Long moduleId){

        Result result = new Result();
        
        try {
            
            Module module = moduleService.load(Module.class, moduleId);
            
            moduleService.update(module.disable());
            
            result.success().message("删除文章模块成功");
            
        } catch (Exception e) {
            
            logger.error("ModuleController#onDelete() error:", e);
            
        }
        
        return JsonUtils.toJsonString(result);
    }

}
